//
//  main.m
//  FileAccessTestProject2
//
//  Created by Paul Thompson on 8/11/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <Foundation/NSObject.h>
#import <Foundation/NSString.h>
#import <Foundation/NSFileHandle.h>
#import <Foundation/NSFileManager.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSData.h>

int main (int argc, const char * argv[]) 
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	NSFileHandle	*inFile, *outFile;
	NSData	*buffer;
	NSString *inFilePath,*outFilePath;
	//path =[[NSFileManager defaultManager] currentDirectoryPath]; 
	inFilePath = [[NSBundle mainBundle] pathForResource:@"NuclideData" ofType:@"csv"];
	outFilePath = [[inFilePath stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Outfile.csv"];
	//NSData *myData = [NSData dataWithContentsOfFile:filePath]; 
	NSLog(@"inFilePath: %@",inFilePath);
	NSLog(@"outFilePath: %@",outFilePath);

	//inFile = [NSFileHandle fileHandleForReadingAtPath: @"NuclideData.csv"];
	inFile = [NSFileHandle fileHandleForReadingAtPath: inFilePath];
	if(inFile==nil){
		NSLog(@"Open of file for read failed");
		//return 1;
	}
	[[NSFileManager defaultManager] createFileAtPath:outFilePath
											contents:nil
										  attributes:nil];
	
	outFile = [NSFileHandle fileHandleForWritingAtPath:outFilePath];
	
	if (outFile==nil) {
		NSLog(@"failed to open outfile for writing");
		return 2;
	}
	
	[outFile truncateFileAtOffset:0];
	buffer = [inFile readDataToEndOfFile];
	[outFile writeData:buffer];
	[inFile closeFile];
	[outFile closeFile];
	NSLog(@"%@",[NSString stringWithContentsOfFile:@"Outfile.csv"
										  encoding: NSUTF8StringEncoding
											 error:nil]);
	
    [pool drain];
    return 0;
}
